<template>
  <div class="session-audit-container">
    <ContentWrap class="audit-wrapper">
      <!-- 页面头部 -->
      <div class="page-header">
        <div class="header-icon">
          <Icon icon="ep:chat-dot-round" />
        </div>
        <div class="header-content">
          <h3 class="page-title">会话审计</h3>
          <p class="page-subtitle">查看和管理企业微信会话记录</p>
        </div>
      </div>

      <!-- 标签页导航 -->
      <div class="tab-navigation">
        <div class="tab-container">
          <div
            class="tab-item"
            v-for="item in menuList"
            :key="item.id"
            :class="{ 'tab-active': menuIndex == item.id }"
            @click="changeItem(item.id)"
          >
            <Icon :icon="item.id === 2 ? 'ep:user' : 'ep:chat-dot-round'" class="tab-icon" />
            <span class="tab-text">{{ item.name }}</span>
          </div>
        </div>
      </div>

      <!-- 提示信息 -->
      <div class="notice-banner">
        <Icon icon="ep:info-filled" class="notice-icon" />
        <span class="notice-text">
          聊天记录会定时清理，记录保留时间可在【系统-企业微信-企业主页】中配置
        </span>
      </div>

      <!-- 搜索区域 -->
      <div class="search-section">
        <div class="search-container">
          <el-input v-model="searchVal" clearable placeholder="请输入名称搜索" class="search-input">
            <template #prefix>
              <Icon icon="ep:search" class="search-prefix-icon" />
            </template>
            <template #append>
              <el-button :icon="Search" @click="searchBtn" class="search-btn" />
            </template>
          </el-input>
        </div>
      </div>

      <!-- 表格区域 -->
      <div class="table-section">
        <div class="table-container">
          <el-table
            :data="list"
            border
            class="audit-table"
            :header-cell-style="{ background: '#f8f9ff', color: '#303133', fontWeight: '600' }"
          >
            <!-- 员工会话表格 -->
            <template v-if="menuIndex == 2">
              <el-table-column align="center" prop="weUserId" label="员工用户名" min-width="150">
                <template #default="scope">
                  <div class="user-cell">
                    <Icon icon="ep:user" class="cell-icon" />
                    <span class="masked-text">{{ maskString(scope.row.weUserId) }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column align="center" prop="nickname" label="系统用户名" min-width="120">
                <template #default="scope">
                  <div class="nickname-cell">
                    <span class="nickname-text">{{ scope.row.nickname || '-' }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column align="center" label="客户数量" width="100">
                <template #default="scope">
                  <el-link @click="showDrawer(scope.row)" type="primary" class="count-link">
                    <Icon icon="ep:user-filled" class="link-icon" />
                    {{ scope.row.customerCount || 0 }}
                  </el-link>
                </template>
              </el-table-column>

              <el-table-column align="center" label="客户群数量" width="110">
                <template #default="scope">
                  <el-link @click="showDrawer(scope.row)" type="primary" class="count-link">
                    <Icon icon="ep:chat-dot-round" class="link-icon" />
                    {{ scope.row.chatGroupCount || 0 }}
                  </el-link>
                </template>
              </el-table-column>

              <el-table-column
                label="上次聊天"
                align="center"
                prop="lastChatTime"
                :formatter="dateFormatter"
                show-overflow-tooltip
                width="180"
              >
                <template #default="scope">
                  <div class="time-cell">
                    <Icon icon="ep:clock" class="time-icon" />
                    <span>{{ dateFormatter(scope.row, null, scope.row.lastChatTime) || '-' }}</span>
                  </div>
                </template>
              </el-table-column>
            </template>

            <!-- 群会话表格 -->
            <template v-if="menuIndex == 3">
              <el-table-column align="center" prop="name" label="群名称" min-width="200">
                <template #default="scope">
                  <div class="group-name-cell">
                    <Icon icon="ep:chat-dot-round" class="cell-icon" />
                    <el-link @click="showDrawer(scope.row)" type="primary" class="group-link">
                      {{ scope.row.name || '-' }}
                    </el-link>
                  </div>
                </template>
              </el-table-column>

              <el-table-column align="center" prop="ownerNickname" label="群主" width="120">
                <template #default="scope">
                  <div class="owner-cell">
                    <Icon icon="ep:crown" class="owner-icon" />
                    <span>{{ scope.row.ownerNickname || '-' }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column align="center" label="成员数量" width="100">
                <template #default="scope">
                  <div class="member-count">
                    <Icon icon="ep:user-filled" class="count-icon" />
                    <span class="count-text">
                      {{ scope.row.memberList ? scope.row.memberList.length : 0 }}
                    </span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                label="上次聊天"
                align="center"
                prop="lastChatTime"
                :formatter="dateFormatter"
                show-overflow-tooltip
                width="180"
              >
                <template #default="scope">
                  <div class="time-cell">
                    <Icon icon="ep:clock" class="time-icon" />
                    <span>{{ dateFormatter(scope.row, null, scope.row.lastChatTime) || '-' }}</span>
                  </div>
                </template>
              </el-table-column>

              <el-table-column
                label="创建时间"
                align="center"
                prop="createTime"
                :formatter="dateFormatter"
                show-overflow-tooltip
                width="180"
              >
                <template #default="scope">
                  <div class="time-cell">
                    <Icon icon="ep:calendar" class="time-icon" />
                    <span>{{ dateFormatter(scope.row, null, scope.row.createTime) || '-' }}</span>
                  </div>
                </template>
              </el-table-column>
            </template>

            <!-- 操作列 -->
            <el-table-column label="操作" align="center" width="100" fixed="right">
              <template #default="scope">
                <el-button type="primary" @click="showDrawer(scope.row)" link class="action-btn">
                  <Icon icon="ep:view" class="btn-icon" />
                  查看
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>

      <!-- 分页 -->
      <div class="pagination-section">
        <Pagination
          :total="totals"
          :page="params.pageNo"
          :limit="params.pageSize"
          @pagination="getList"
        />
      </div>
    </ContentWrap>

    <Details ref="drawer" />
  </div>
</template>

<script setup lang="ts">
import Details from './components/drawer.vue'
import * as customerApi from '@/api/wecom/customer/index'
import * as userApi from '@/api/wecom/user/index'
import * as chatGroupApi from '@/api/wecom/chatgroup/index'
import { dateFormatter } from '@/utils/formatTime'
import { Search } from '@element-plus/icons-vue'

const menuList = [
  {
    name: '员工会话',
    id: 2
  },
  {
    name: '群会话',
    id: 3
  }
]

const list = ref([])
const menuIndex = ref(2)
const drawer = ref(null)
const searchVal = ref('')
const params = ref({
  pageNo: 1,
  pageSize: 10
})
const totals = ref(0)

// 搜索按钮
const searchBtn = () => {
  params.value.pageNo = 1
  if (menuIndex.value == 1) {
    customerPages()
  } else if (menuIndex.value == 2) {
    userPages()
  } else {
    chatGroupPages()
  }
}

onMounted(() => {
  userPages()
})

const getList = (val) => {
  params.value.pageNo = val.page
  params.value.pageSize = val.limit
  if (menuIndex.value == 1) {
    customerPages()
  } else if (menuIndex.value == 2) {
    userPages()
  } else {
    chatGroupPages()
  }
}

const changeItem = (val) => {
  params.value.pageNo = 1
  menuIndex.value = val
  if (val == 1) {
    customerPages()
  } else if (val == 2) {
    userPages()
  } else {
    chatGroupPages()
  }
}

// 客户列表
const customerPages = async () => {
  list.value = await customerApi.getWeCustomerSimpleList({ name: searchVal.value, ...params.value })
}

// 员工列表
const userPages = async () => {
  const result = await userApi.getPageWeUserList({
    nickname: searchVal.value,
    ...params.value,
    showCount: false,
    isAll: 1
  })
  list.value = result.list
  totals.value = result.total
}

// 群列表
const chatGroupPages = async () => {
  const result = await chatGroupApi.getWeChatGroupPage({ name: searchVal.value, ...params.value })
  list.value = result.list
  totals.value = result.total
}

const showDrawer = (item) => {
  drawer.value.open(true, item, menuIndex.value)
}

/*处理id*/
const maskString = (str) => {
  if (!str || str.length !== 32) {
    return str
  }
  const start = str.slice(0, 3)
  const end = str.slice(-3)
  return `${start}******${end}`
}
</script>

<style scoped lang="scss">
// 主题色变量
$primary-color: #0052cc;
$primary-light: rgba(0, 82, 204, 0.1);
$primary-border: rgba(0, 82, 204, 0.3);
$success-color: #67c23a;
$warning-color: #e6a23c;
$danger-color: #f56c6c;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$text-placeholder: #c0c4cc;
$border-color: #dcdfe6;
$border-light: #e4e7ed;
$bg-color: #f5f7fa;
$shadow-light: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
$shadow-base:
  0 2px 4px rgba(0, 0, 0, 0.12),
  0 0 6px rgba(0, 0, 0, 0.04);
$border-radius: 12px;
$border-radius-small: 8px;

.session-audit-container {
  padding: 16px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  min-height: 100vh;

  @media (max-width: 768px) {
    padding: 8px;
  }
}

.audit-wrapper {
  border-radius: $border-radius;
  overflow: hidden;
  box-shadow: $shadow-light;
  border: 1px solid $border-light;
  background: white;

  :deep(.el-card__body) {
    padding: 0;
  }
}

/* 页面头部 */
.page-header {
  display: flex;
  align-items: center;
  padding: 12px 24px;
  background: linear-gradient(135deg, $primary-color 0%, #1d4ed8 100%);
  color: white;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: $border-radius-small;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    width: 40px;
    height: 40px;
    font-size: 18px;
  }
}

.header-content {
  flex: 1;
  margin-left: 16px;

  @media (max-width: 768px) {
    margin-left: 0;
  }
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0 0 4px 0;

  @media (max-width: 768px) {
    font-size: 18px;
  }
}

.page-subtitle {
  font-size: 14px;
  opacity: 0.9;
  margin: 0;

  @media (max-width: 768px) {
    font-size: 12px;
  }
}

/* 标签页导航 */
.tab-navigation {
  padding: 24px 24px 0;
  background: white;

  @media (max-width: 768px) {
    padding: 16px 16px 0;
  }
}

.tab-container {
  display: flex;
  justify-content: center;
  background: #f8f9ff;
  border-radius: $border-radius-small;
  padding: 4px;
  border: 1px solid $border-light;

  @media (max-width: 768px) {
    flex-direction: row;
    overflow-x: auto;
    justify-content: flex-start;
  }
}

.tab-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 14px;
  font-weight: 500;
  color: $text-secondary;
  background: transparent;
  border: 1px solid transparent;
  flex: 1;
  justify-content: center;
  min-width: 120px;

  @media (max-width: 768px) {
    padding: 10px 16px;
    font-size: 13px;
    min-width: 100px;
    flex-shrink: 0;
  }

  &:hover {
    color: $primary-color;
    background: rgba(0, 82, 204, 0.05);
  }

  &.tab-active {
    background: $primary-color;
    color: white;
    border-color: $primary-color;
    box-shadow: 0 2px 8px rgba(0, 82, 204, 0.3);
  }
}

.tab-icon {
  font-size: 16px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.tab-text {
  @media (max-width: 480px) {
    display: none;
  }
}

/* 提示横幅 */
.notice-banner {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  background: linear-gradient(135deg, #fff3cd 0%, #ffeaa7 100%);
  border-bottom: 1px solid #f0c674;
  color: #856404;

  @media (max-width: 768px) {
    padding: 10px 16px;
    flex-direction: column;
    text-align: center;
    gap: 6px;
  }
}

.notice-icon {
  color: #f39c12;
  font-size: 16px;
  flex-shrink: 0;

  @media (max-width: 768px) {
    font-size: 14px;
  }
}

.notice-text {
  font-size: 12px;
  line-height: 1.4;

  @media (max-width: 768px) {
    font-size: 11px;
  }
}

/* 搜索区域 */
.search-section {
  padding: 24px;
  background: white;
  border-bottom: 1px solid $border-light;

  @media (max-width: 768px) {
    padding: 16px;
  }
}

.search-container {
  max-width: 400px;

  @media (max-width: 768px) {
    max-width: 100%;
  }
}

.search-input {
  :deep(.el-input__wrapper) {
    border-radius: $border-radius-small;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }

  :deep(.el-input-group__append) {
    border-radius: 0 $border-radius-small $border-radius-small 0;
    border-left: none;
    background: $primary-color;
    border-color: $primary-color;

    .search-btn {
      background: transparent;
      border: none;
      color: white;
      padding: 0 12px;

      &:hover {
        background: rgba(255, 255, 255, 0.1);
      }
    }
  }
}

.search-prefix-icon {
  color: $text-placeholder;
  font-size: 14px;
}

/* 表格区域 */
.table-section {
  padding: 12px 24px;
  background: white;

  @media (max-width: 768px) {
    padding: 0 16px 16px;
  }
}

.table-container {
  border-radius: $border-radius-small;
  overflow: hidden;
  border: 1px solid $border-light;
  box-shadow: $shadow-base;
}

.audit-table {
  :deep(.el-table__header) {
    th {
      background: linear-gradient(135deg, #f8f9ff 0%, #f0f2ff 100%) !important;
      color: $text-primary !important;
      font-weight: 600 !important;
      border-bottom: 2px solid $primary-light;
      padding: 16px 12px;
    }
  }

  :deep(.el-table__body) {
    tr {
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light !important;
      }

      td {
        padding: 14px 12px;
        border-bottom: 1px solid $border-light;
      }
    }

    .el-table__row--striped {
      background: #fafbff;

      &:hover {
        background: $primary-light !important;
      }
    }
  }

  // H5适配
  @media (max-width: 768px) {
    :deep(.el-table__header) th {
      padding: 12px 8px;
      font-size: 13px;
    }

    :deep(.el-table__body) td {
      padding: 12px 8px;
      font-size: 12px;
    }
  }
}

/* 表格单元格样式 */
.user-cell,
.group-name-cell,
.owner-cell,
.time-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  justify-content: center;
}

.cell-icon,
.time-icon {
  color: $primary-color;
  font-size: 14px;
  flex-shrink: 0;
}

.owner-icon {
  color: #f39c12;
  font-size: 14px;
  flex-shrink: 0;
}

.masked-text {
  font-family: 'Courier New', monospace;
  font-size: 12px;
  color: $text-regular;
  background: #f5f7fa;
  padding: 2px 6px;
  border-radius: 4px;
}

.nickname-cell {
  text-align: center;
}

.nickname-text {
  color: $text-primary;
  font-weight: 500;
}

.count-link {
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: 600;
  transition: all 0.3s ease;

  &:hover {
    transform: scale(1.05);
  }
}

.link-icon {
  font-size: 12px;
}

.group-link {
  font-weight: 500;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;

  @media (max-width: 768px) {
    max-width: 100px;
  }
}

.member-count {
  display: flex;
  align-items: center;
  gap: 4px;
  justify-content: center;
}

.count-icon {
  color: $success-color;
  font-size: 14px;
}

.count-text {
  font-weight: 600;
  color: $text-primary;
}

.action-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s ease;

  &:hover {
    background: $primary-light;
    transform: scale(1.05);
  }
}

.btn-icon {
  font-size: 12px;
}

/* 分页区域 */
.pagination-section {
  padding: 20px 24px;
  background: white;
  border-top: 1px solid $border-light;
  display: flex;
  justify-content: center;

  @media (max-width: 768px) {
    padding: 16px;
  }

  :deep(.el-pagination) {
    .el-pager li {
      border-radius: 6px;
      margin: 0 2px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }

      &.is-active {
        background: $primary-color;
        color: white;
      }
    }

    .btn-prev,
    .btn-next {
      border-radius: 6px;
      transition: all 0.3s ease;

      &:hover {
        background: $primary-light;
        color: $primary-color;
      }
    }

    @media (max-width: 768px) {
      .el-pagination__sizes,
      .el-pagination__jump {
        display: none;
      }
    }
  }
}

/* 响应式优化 */
@media (max-width: 480px) {
  .session-audit-container {
    padding: 4px;
  }

  .tab-navigation,
  .search-section,
  .table-section {
    padding-left: 12px;
    padding-right: 12px;
  }

  .page-header {
    padding: 12px;
  }

  .notice-banner {
    padding: 8px 12px;
  }

  .tab-item {
    padding: 8px 12px;
    font-size: 12px;
  }

  .masked-text {
    font-size: 10px;
    padding: 1px 4px;
  }

  .group-link {
    max-width: 80px;
  }

  .pagination-section {
    padding: 12px;
  }
}

/* 加载状态优化 */
:deep(.el-loading-mask) {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .path {
      stroke: $primary-color;
    }
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .session-audit-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .audit-wrapper {
    background: #374151;
    border-color: #4b5563;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  }

  .tab-navigation,
  .search-section,
  .table-section,
  .pagination-section {
    background: #374151;
  }

  .page-title {
    color: #f9fafb;
  }

  .tab-container {
    background: #4b5563;
    border-color: #6b7280;
  }

  .tab-item {
    color: #d1d5db;

    &:hover {
      color: #60a5fa;
      background: rgba(96, 165, 250, 0.1);
    }

    &.tab-active {
      background: #60a5fa;
      color: white;
      border-color: #60a5fa;
    }
  }

  .notice-banner {
    background: #4b5563;
    border-color: #6b7280;
    color: #d1d5db;
  }

  .audit-table {
    :deep(.el-table__header) {
      th {
        background: #4b5563 !important;
        color: #f9fafb !important;
        border-bottom-color: #6b7280;
      }
    }

    :deep(.el-table__cell) {
      border-bottom-color: #4b5563;
    }
  }

  .masked-text {
    background: #4b5563;
    color: #d1d5db;
  }

  .nickname-text,
  .count-text {
    color: #f9fafb;
  }
}
</style>
